/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.lightswitch.smarttranslator.translator;

import java.io.BufferedReader;
import java.io.InputStreamReader;
import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.util.JSONTokener;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import sun.misc.IOUtils;

/**
 *
 * @author Jun
 */
public class TranslatorHelper {
    public static String Translate(String inputString, String inLang, String outLang) {
        String outputString = "";
        String input = new String(inputString.trim().replace(" ", "%20").replace("&", "%26")
                .replace(",", "%2c").replace("(", "%28").replace(")", "%29")
                .replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
                .replace(">", "%3E").replace("#", "%23").replace("$", "%24")
                .replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
                .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
                .replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
                .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
                .replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
                .replace("|", "%7C").replace("}", "%7D"));
        String request = "http://api.mymemory.translated.net/get?q=" + input + "&langpair="+inLang+"%7C"+outLang;

        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpGet httpGetRequest = new HttpGet(request);
            HttpResponse httpResponse = httpClient.execute(httpGetRequest);
            //JSONObject json = Util.parseJSon(httpResponse);
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();
            JSONObject jsonObject = JSONObject.fromObject(json);
            String responseDataJson = jsonObject.getString("responseData");
            JSONObject responseDataJObject = JSONObject.fromObject(responseDataJson);
            outputString = responseDataJObject.getString("translatedText");
        } catch (Exception e) {
        }
        return outputString;
    }
    public static String TranslateWithDefaultLanguage(String inputString) {
        String outputString = "";
        String input = new String(inputString.trim().replace(" ", "%20").replace("&", "%26")
                .replace(",", "%2c").replace("(", "%28").replace(")", "%29")
                .replace("!", "%21").replace("=", "%3D").replace("<", "%3C")
                .replace(">", "%3E").replace("#", "%23").replace("$", "%24")
                .replace("'", "%27").replace("*", "%2A").replace("-", "%2D")
                .replace(".", "%2E").replace("/", "%2F").replace(":", "%3A")
                .replace(";", "%3B").replace("?", "%3F").replace("@", "%40")
                .replace("[", "%5B").replace("\\", "%5C").replace("]", "%5D")
                .replace("_", "%5F").replace("`", "%60").replace("{", "%7B")
                .replace("|", "%7C").replace("}", "%7D"));
        String request = "http://api.mymemory.translated.net/get?q=" + input + "&langpair=en%7Cvi";

        HttpClient httpClient = new DefaultHttpClient();
        try {
            HttpGet httpGetRequest = new HttpGet(request);
            HttpResponse httpResponse = httpClient.execute(httpGetRequest);
            //JSONObject json = Util.parseJSon(httpResponse);
            BufferedReader reader = new BufferedReader(new InputStreamReader(httpResponse.getEntity().getContent(), "UTF-8"));
            String json = reader.readLine();
            JSONObject jsonObject = JSONObject.fromObject(json);
            String responseDataJson = jsonObject.getString("responseData");
            JSONObject responseDataJObject = JSONObject.fromObject(responseDataJson);
            outputString = responseDataJObject.getString("translatedText");
        } catch (Exception e) {
        }
        return outputString;
    }
}
